package com.cincc.cincc_pbx.ui;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.cincc.cincc_pbx.R;
import com.cincc.cincc_pbx.common.MyUtils;
import com.cincc.cincc_pbx.constant.CallConstant;
import com.gyf.immersionbar.ImmersionBar;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

public class MineFragment extends Fragment implements View.OnClickListener {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        ImageView imageView = view.findViewById(R.id.user_avatar_img);
        int statusBarHeight = ImmersionBar.getStatusBarHeight(this);
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) imageView.getLayoutParams();
        lp.topMargin = statusBarHeight + 20;
        imageView.setLayoutParams(lp);

        TextView userText = view.findViewById(R.id.user_name_tv);
        userText.setText(CallConstant.loginModel.getUserName());

        RelativeLayout quitRelayout = view.findViewById(R.id.quit_relayout);
        RelativeLayout versionRelayout = view.findViewById(R.id.version_relayout);
        ImageView avatarImage = view.findViewById(R.id.user_avatar_img);
        quitRelayout.setOnClickListener(this);
        avatarImage.setOnClickListener(this);
        versionRelayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.quit_relayout) {
            quitWarn();
        }
        if (v.getId() == R.id.user_avatar_img) {
            showExtension();
        }
        if (v.getId() == R.id.version_relayout) {
            showVersion();
        }
    }

    private void showExtension() {
        String sipAccount = MyUtils.getSipAccount();
        new XPopup.Builder(requireActivity())
                .asConfirm("分机号", sipAccount, "", "确定",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                // 确定按钮点击事件
                            }
                        }, null, true)
                .show();
    }

    private void quitWarn() {
        new XPopup.Builder(getContext()).asConfirm("提示", "确定要退出登录吗？",
                        () -> {
                            Intent i = new Intent(requireActivity(), LoginActivity.class);
                            requireActivity().startActivity(i);
                            requireActivity().finish();
                        })
                .show();
    }

    private void showVersion() {
        String versionName = "";
        try {
            PackageInfo packageInfo = requireActivity().getPackageManager().getPackageInfo(requireActivity().getPackageName(), 0);
            versionName = packageInfo.versionName;
            new XPopup.Builder(requireActivity())
                    .asConfirm("版本号", versionName, "", "确定",
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    // 确定按钮点击事件
                                }
                            }, null, true)
                    .show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
